//调用handler
//handler 在当前目录可以直接 super找到
use super::handler::{Handler,PageNotFoundHandler,StaticPageHandler,WebServiceHandler};

use http::{httprequest,httprequest::HttpRequest,httpresponse,httpresponse::HttpResponse};
use std::io::prelude::*;

pub struct Router;

impl Router{
    pub fn router(req:HttpRequest,stream:&mut impl Write)->(){
        match req.method{
            //若当前是get请求 继续match资源
            httprequest::Method::Get =>match &req.resource{
                httprequest::Resource::Path(s)=>{
                    let router:Vec<_>=s.split("/").collect();
                    match router[1]{
                        "api"=>{
                            let resp=WebServiceHandler::handler(&req);
                            let _=resp.send_response(stream);
                        }
                        _=>{
                            let resp=StaticPageHandler::handler(&req);
                            let _=resp.send_response(stream);
                        }
                    }
                }
            }
            _=>{
                let resp=PageNotFoundHandler::handler(&req);
                let _=resp.send_response(stream);
            }
        }
    }
}
